import { checkStr } from "@/utils/judge";
import store from "@/store";
import url from "@/axios/url";
const { mesUrl, adminUrl } = url();
import { deleteFile } from "@/api/common";
import { getDict } from "@/api/common";
import {
  mesTmMaterialBrandPage
} from "@/api/materials/brand";
import { materialBaseOptions } from "@/api/supplierManagement/supplierManagement";
// const { mesUrl } = url();
let vm = null;
const sendThis = (_this) => {
  vm = _this;
};
export { sendThis };


const validatePrice = (rule, value, callback) => {
  console.log(value)
  if(/(^[1-9]\d*(\.\d{1})?$)|(^0(\.\d{1})?$)/.test(value)) {
    if(Number(value) > 100000) {
      callback(new Error('单价不能超过10万'));
    } else {
      callback()
    }
  } else {
    callback(new Error('请输入正确格式的价格(保留一位小数)'));
  } 
};
const validateFreight = (rule, value, callback) => {
  if(/(^[1-9]\d*(\.\d{1})?$)|(^0(\.\d{1})?$)/.test(value)) {
    if(Number(value) > 10000) {
      callback(new Error('运费不能超过1万'));
    } else {
      callback()
    }
  } else {
    callback(new Error('请输入正确格式的运费(保留一位小数)'));
  }
};

const validateArrivalDays = (rule, value, callback) => {
  console.log(Number(value) % 0.5)
  if(/(^[1-9]\d*(\.\d{1})?$)|(^0(\.\d{1})?$)/.test(value) && ((Number(value) % 0.5) === 0)) {
    callback()
  } else {
    callback(new Error('到货天数请输入正数,最小时间度为0.5天'));
  } 
};

var validatePass = (rule, value, callback) => {
  setTimeout(() => {
    if (!checkStr(value, "english-number")) {
      callback(new Error($i18n.t("mesProcess.rule8")));
    } else {
      callback();
    }
  }, 100);
};
// 立即报价表单配置
export const formDescDialogMinxin = {
  data () {
    const self = this;
    const baseFormConfig = {
      span: 24,
      disabled: false,
      inline: true,
      isShowSubmitBtn: false,
      isShowBackBtn: false,
      rules: {
        brand: [
          { max: 20, message: $i18n.t("common.overlongPrompt", { length: 20 }), trigger: ["change", "blur"] }
        ],
        unitPrice: [
          { validator: validatePrice, trigger: ["change", "blur"] },
          // { max: 30, message: $i18n.t("common.overlongPrompt", { length: 30 }), trigger: ["change", "blur"] }
        ],
        freight: [
          { validator: validateFreight, trigger: ["change", "blur"] },
          // { max: 30, message: $i18n.t("common.overlongPrompt", { length: 30 }), trigger: ["change", "blur"] }
        ],
        quantity: [
          // { max: 30, message: $i18n.t("common.overlongPrompt", { length: 30 }), trigger: ["change", "blur"] }
        ],
        totalPrice: [
          // { max: 30, message: $i18n.t("common.overlongPrompt", { length: 30 }), trigger: ["change", "blur"] }
        ],
        arrivalDays: [
          { validator: validateArrivalDays, trigger: ["change", "blur"] },
        ],
        deliveryTime: [
          { max: 150, message: $i18n.t("common.overlongPrompt", { length: 150 }), trigger: ["change", "blur"] }
        ],
        deliveryMode: [
          { max: 20, message: $i18n.t("common.overlongPrompt", { length: 20 }), trigger: ["change", "blur"] }
        ],
        remarks: [
          { max: 150, message: $i18n.t("common.overlongPrompt", { length: 150 }), trigger: ["change", "blur"] }
        ],
      },
      formDesc: {
        unitPrice: {
          type: "input",
          label: $i18n.t("supplier.unitPrice"),
          layout: 12,
          required: true,
          attrs: {
            type: 'number'
          },
          on: {
            change: (val) => {
              let data = self.currentFormData
              self.$set(data, 'totalPrice', (Number(val)*Number(data.num) + (data.freight ? Number(data.freight) : 0)).toFixed(2))
            },
          },
        },
        freight: {
          type: "input",
          label: $i18n.t("supplier.freight"),
          layout: 12,
          attrs: {
            type: 'number'
          },
          required: true,
          on: {
            change: (val) => {
              let data = self.currentFormData
              self.$set(data, 'totalPrice', (Number(data.unitPrice)*Number(data.num) +  Number(val)).toFixed(2))
            },
          },
        },
        num: {
          type: "input",
          label: $i18n.t("supplier.quantity"),
          layout: 12,
          required: true,
          disabled: true,
          attrs: {
            type: 'number'
          },
        },
        totalPrice: {
          type: "input",
          label: $i18n.t("supplier.totalPrice"),
          layout: 12,
          attrs: {
            type: 'number'
          },
          disabled: true,
        },
        // arrivalDays: {
        //   type: "datetime",
        //   attrs: {
        //     valueFormat: "yyyy-MM-dd HH",
        //   },
        //   label: $i18n.t("supplier.deliveryTime"),
        //   layout: 24,
        //   required: true,
        //   disabled: (d) => !!d.id,
        // },
        arrivalDays: {
          type: "input",
          label: $i18n.t("supplier.arrivalDays"),
          layout: 12,
          required: true,
          attrs: {
            type: 'number'
          },
        },
        deliveryMode: {
          type: "input",
          label: $i18n.t("supplier.deliveryMethod"),
          layout: 12,
          required: true,
        },
        remarks: {
          type: "textarea",
          label: $i18n.t("supplier.remarks"),
          layout: 24,
          required: false,
        },
        pictureList: {
          type: "image-uploader",
          label: $i18n.t("supplier.uploadImages"),
          layout: 24,
          required: false,
          attrs: {
            limit: 5,
            fileSize: 3, // 文件大小限制 单位M
            // fileType: ["png"],
            multiple: true, // 是否支持多选
            // disabled: true,
            size: 60, // 图片显示大小
            action: `${adminUrl}file`,
            data: {
              bucket: "messervice",
            },
            headers: {
              Satoken: store.getters.token,
            },
            beforeRemove: (data, list) => {
              const bucket = "messervice";
              const filename = data.title;
              console.log(filename)
              return new Promise((resolve, reject) => {
                deleteFile(`${adminUrl}file`, {
                  bucket,
                  filename,
                })
                  .then((res) => {
                    resolve(res.data);
                  })
                  .catch((error) => {
                    reject(error);
                  });
              });
            },
            responseFn(response, file) {
              return {
                src: response.result.path,
                // title: response.result.object,
                ...response.result,
              };
            },
          },
        },
      }
    }
    return {
      noBrandFormConfig: {
        ...baseFormConfig,
        formDesc: {
          ...baseFormConfig.formDesc,
          pictureList: {
            type: "image-uploader",
            label: $i18n.t("supplier.uploadImages"),
            layout: 24,
            required: true,
            attrs: {
              limit: 5,
              fileSize: 3, // 文件大小限制 单位M
              // fileType: ["png"],
              multiple: true, // 是否支持多选
              // disabled: true,
              size: 60, // 图片显示大小
              action: `${adminUrl}file`,
              data: {
                bucket: "messervice",
              },
              headers: {
                Satoken: store.getters.token,
              },
              beforeRemove: (data, list) => {
                const bucket = "messervice";
                const filename = data.title;
                console.log(filename)
                return new Promise((resolve, reject) => {
                  deleteFile(`${adminUrl}file`, {
                    bucket,
                    filename,
                  })
                    .then((res) => {
                      resolve(res.data);
                    })
                    .catch((error) => {
                      reject(error);
                    });
                });
              },
              responseFn(response, file) {
                return {
                  src: response.result.path,
                  // title: response.result.object,
                  ...response.result,
                };
              },
            },
          }
        }
      },
      hasBrandFormConfig: {
        ...baseFormConfig,
        formDesc: {
          brand: {
            type: "select",
            label: $i18n.t("supplier.brand"),
            layout: 12,
            required: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "id",
              text: "brand",
            },
            disabled: true,
            options: async (data) => {
              return []
              // console.log('ssss',data)
              // const list = await mesTmMaterialBrandPage({
              //   pageNum: 1,
              //   pageSize: 1000000,
              //   materialCode: self.materialCode
              // });
              // return list.data.result.records;
            },
          },
          materialUnit: {
            type: "input",
            label: '规格',
            layout: 12,
            required: true,
            disabled: true
          },
          ...baseFormConfig.formDesc
        },
      },
    };
  },
};

